package com.zry.yard.ui.activity

import android.content.Intent
import android.view.KeyEvent
import android.view.View

import com.zry.yard.R
import com.zry.yard.been.ResultBean
import com.zry.yard.been.StoreOrderInfo
import com.zry.yard.databinding.ActivityDialogBinding
import com.zry.yard.presenter.PutStorageRecordListPresenter
import com.zry.yard.retrofit.HttpServer
import com.zry.yard.retrofit.ShowUserView
import com.zry.yard.ui.activity.PutStorageRecordInfoActivity.Companion.putStorageRecordInfoActivity
import com.zry.yard.ui.base.BaseNativeActivity
import com.zry.yard.utils.ACacheUtil
import com.zry.yard.utils.KeyboardUtils
import kotlinx.android.synthetic.main.activity_dialog.*


class DialogActivity : BaseNativeActivity(), ShowUserView {

    private var dialogBinding: ActivityDialogBinding? = null
    private var orderID: String? = null

    override fun getLayoutId(): Int {
        return R.layout.activity_dialog
    }

    override fun initData() {
        dialogBinding = viewDataBinding as ActivityDialogBinding
        dialogBinding?.handlerClick = this
        orderID = intent.getStringExtra("orderID")
    }


    fun dialogHandlerClick(view: View) {
        when (view.id) {
            R.id.deleteDialog -> {
                KeyboardUtils().hideKeyboard(this)
                finish()
            }
            R.id.dialogSubmit -> finishOrder()
        }
    }


    private fun finishOrder() {
        HttpServer(this).finishOrderInfo(orderID!!,
                dialogRemarksET.text.toString().trim(),
                ACacheUtil().getLoginInfo().userid, this)
    }


    override fun toMainActivity(what: Int, data: Any) {
        when (what) {
            33 -> {
                val resultData = data as ResultBean<StoreOrderInfo>
                if (resultData.State == "1") {
                    toast("结单成功")
                    KeyboardUtils().hideKeyboard(this)
                    val intent = Intent(this, PutStorageRecordInfoActivity::class.java)
                    intent.putExtra("title", "入库记录")
                    startActivity(intent)
                } else {
                    toast("结单失败")
                }
            }
        }
    }


    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {

        if (keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_DOWN) {
            KeyboardUtils().hideKeyboard(this)
            finish()
            return true
        }
        return super.onKeyDown(keyCode, event)
    }
}
